﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace estiglp1112_5959_csharp
{
    public partial class MainForm : Form
    {
        private String ConnectionString = "Data Source=" + Path.GetDirectoryName(Application.ExecutablePath) + "\\database.db;UseUTF8Encoding=True;Version=3;";
        TableControl tableControl = null;
        StatisticsControl statsControl = null;


        public MainForm()
        {
            InitializeComponent();
        }

        private void showTableToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (statsControl != null)
            {
                this.statsControl.Dispose();
                this.statsControl = null;
            }

            if (tableControl == null)
            {
                this.tableControl = new TableControl(this.contentsPanel, ConnectionString, false);
            } else {
                this.tableControl.EnableSubmitMode = false;
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.statsControl != null)
            {
                this.statsControl.Hide();
                this.statsControl.Dispose();
                this.statsControl = null;
            }

            if (this.tableControl != null)
            {
                this.tableControl.Hide();
                this.tableControl.Dispose();
                this.tableControl = null;
            }
            this.Dispose();
        }

        private void importCSVToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (statsControl != null)
            {
                this.statsControl.Dispose();
                this.statsControl = null;
            }

            if (tableControl == null)
            {
                this.tableControl = new TableControl(this.contentsPanel, ConnectionString, true);
            }
            else if (this.tableControl.EnableSubmitMode != true)
            {
                this.tableControl.EnableSubmitMode = true;
            }
            this.tableControl.importCSV();
        }

        private void statisticsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (tableControl != null)
            {
                this.tableControl.Dispose();
                this.tableControl = null;
            }

            if (statsControl == null)
            {
                this.statsControl = new StatisticsControl(this.contentsPanel, ConnectionString);
            }
        }
    }
}
